package au.com.iglootech.TaxTimeToolTip;

import java.util.Iterator;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.style.TaxTimeToolTipResource;
import au.com.iglootech.TaxTimeToolTip.ui.company.ManageCompanyPage;
import au.com.iglootech.TaxTimeToolTip.ui.feerate.ManageFeeRatePage;
import au.com.iglootech.TaxTimeToolTip.ui.fillupform.FillUpTimeSheetPage;
import au.com.iglootech.TaxTimeToolTip.ui.fillupform.FillUpTimeSheetTab;
import au.com.iglootech.TaxTimeToolTip.ui.fillupform.TodayTimeSheetTab;
import au.com.iglootech.TaxTimeToolTip.ui.invoice.InvoiceTaxPage;
import au.com.iglootech.TaxTimeToolTip.ui.log.LogPage;
import au.com.iglootech.TaxTimeToolTip.ui.payment.PaymentPage;
import au.com.iglootech.TaxTimeToolTip.ui.search.SearchTaskPage;

import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Configurable(preConstruction = true)
@Component
@Scope("session")
public class TaxTimeToolTipMainWindow extends Window {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7937999929345336377L;
	VerticalLayout main = new VerticalLayout();
	private Label userInfo;

	public TaxTimeToolTipMainWindow(String title) {
		super(title);
		main.setSizeFull();
		CssLayout toolbar = new CssLayout();
		toolbar.setWidth("100%");
		toolbar.addStyleName("toolbar-invert");
		CssLayout right = new CssLayout();
		right.setSizeUndefined();
		right.addStyleName("right");
		toolbar.addComponent(right);
		userInfo = new Label("Informational text");
		right.addComponent(userInfo);
		Button b = new Button("Settings");
		b.setIcon(TaxTimeToolTipResource.getSettingsIcon());
		b.addStyleName("borderless");
		right.addComponent(b);
		b = new Button("User");
		b.setIcon(TaxTimeToolTipResource.getUserIcon());
		b.addStyleName("borderless");
		right.addComponent(b);
		CssLayout left = new CssLayout();
		left.setSizeUndefined();
		left.addStyleName("left");
		toolbar.addComponent(left);
		Label title1 = new Label("Tax Time Sheet");
		title1.addStyleName("h1");
		left.addComponent(title1);
		b = new NativeButton("Today's time sheet");
		b.setIcon(TaxTimeToolTipResource.getCalendarIcon());
		b.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent arg0) {
				TodayTimeSheetTab timeSheet = new TodayTimeSheetTab();
				Window timeSheetWindow = new Window();
				timeSheetWindow.setCaption("Today's time sheet");
				timeSheetWindow.setModal(true);
				timeSheetWindow.setWidth("650px");
				timeSheetWindow.setHeight("550px");
				timeSheetWindow.setContent(timeSheet);
				getWindow().addWindow(timeSheetWindow);
			}
		});
		left.addComponent(b);
		b = new NativeButton("Fill Time Sheet");
		b.setIcon(TaxTimeToolTipResource.getAddDocumentIcon());
		b.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent arg0) {
				FillUpTimeSheetTab timeSheet = new FillUpTimeSheetTab();
				Window timeSheetWindow = new Window();
				timeSheetWindow.setCaption("Fill Time Sheet");
				timeSheetWindow.setModal(true);
				setWidth("450px");
				setHeight("400px");
				timeSheetWindow.setContent(timeSheet);
				getWindow().addWindow(timeSheetWindow);
			}
		});
		left.addComponent(b);
		main.addComponent(toolbar);
		final SplitPanel split = new SplitPanel(
				SplitPanel.ORIENTATION_HORIZONTAL);
		split.addStyleName("small blue white");
		split.setSplitPosition(20);
		main.addComponent(split);
		main.setExpandRatio(split, 1);
		CssLayout menu = new CssLayout();
		menu.addStyleName("menu");
		menu.setWidth("100%");
		split.setFirstComponent(menu);
		final Button.ClickListener change = new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				ComponentContainer p = (ComponentContainer) event.getButton()
						.getParent();
				for (Iterator iterator = p.getComponentIterator(); iterator
						.hasNext();) {
					((AbstractComponent) iterator.next())
							.removeStyleName("selected");
				}
				event.getButton().addStyleName("selected");
				switch((PageName)event.getButton().getData())
				{
					case LOG_PAGE:
						split.setSecondComponent(new LogPage());
						break;
					case MANAGE_COMPANY_PAGE:
						split.setSecondComponent(new ManageCompanyPage());
						break;
					case MANAGE_FEE_RATE_PAGE:
						split.setSecondComponent(new ManageFeeRatePage());
						break;
					case SEARCH_PAGE:
						split.setSecondComponent(new SearchTaskPage());
						break;
					case TASK_PAGE:
						split.setSecondComponent(new FillUpTimeSheetPage());
						break;
					case TAX_INVOICE_PAGE:
						split.setSecondComponent(new InvoiceTaxPage());
						break;
					case PAYMENT_PAGE:
						split.setSecondComponent(new PaymentPage());
						break;
				}
			}
		};
		Label l = new Label("Memu");
		l.addStyleName("section");
		menu.addComponent(l);
		// fill up time sheet
		b = new NativeButton("Fill Time Sheet", change);
		b.addStyleName("selected");
		menu.addComponent(b);
		b.setData(PageName.TASK_PAGE);
		split.setSecondComponent(new FillUpTimeSheetPage());

		// tax invoice
		b = new NativeButton("Print Invoice", change);
		menu.addComponent(b);
		b.setData(PageName.TAX_INVOICE_PAGE);
		
		b = new NativeButton("Payment", change);
		menu.addComponent(b);
		b.setData(PageName.PAYMENT_PAGE);

		// manage company
		b = new NativeButton("Manage Company", change);
		menu.addComponent(b);
		b.setData(PageName.MANAGE_COMPANY_PAGE);

		// manage fee rate
		b = new NativeButton("Manage FeeRate", change);
		menu.addComponent(b);
		b.setData(PageName.MANAGE_FEE_RATE_PAGE);

		// search page
		b = new NativeButton("Search", change);
		menu.addComponent(b);
		b.setData(PageName.SEARCH_PAGE);

		// search page
		b = new NativeButton("Log", change);
		menu.addComponent(b);
		b.setData(PageName.LOG_PAGE);

		l = new Label("This application is for the Brilliant Ltd.");
		menu.addComponent(l);
		Label cp = new Label("@Copy right 2012.");
		menu.addComponent(cp);
		setContent(main);
	}

	public void setUserInfo(String content) {
		userInfo.setValue("welcome, " + content);
	}
}
